package com.allways.dubbo.mdm.service.vehicleCate.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.allways.dubbo.mdm.exception.MdmException;
import com.allways.dubbo.mdm.exception.MdmExceptionCode;
import com.allways.dubbo.mdm.service.BaseDataUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import com.allways.dubbo.base.dao.MdmBaseDaoImpl;
import com.allways.dubbo.mdm.beans.vehicleCate.VehicleCate;
import com.allways.dubbo.mdm.service.vehicleCate.IVehicleCateService;

/**
 * <p>ClassName: IVehicleCateService</p>
 * <p>Description: 车型大类Service接口</p>
 * <p>Author: weih</p>
 * <p>Date: 2016-09-21</p>
 */
@Service("mdmVehicleCateService")
public class VehicleCateServiceImpl extends MdmBaseDaoImpl implements IVehicleCateService {
	Log log = LogFactory.getLog(VehicleCateServiceImpl.class);

    @Autowired
    BaseDataUtils baseDataUtils;
    
    @Override
    public int addVehicleCate(VehicleCate vehicleCate) {
   		log.debug("For the new vehicleCate parameters:"+ vehicleCate.toJson());
   		validColumnLen(vehicleCate);
    	
        //无code的不能插入
        if(StringUtils.isEmpty(vehicleCate.getVehicleCateCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code重复的不允许插入
        if(null != getVehicleCateByCode(vehicleCate.getVehicleCateCode())){
            throw new MdmException(MdmExceptionCode.CODE_REPEATED,null);
        }
        
        return insert("vehicleCate.insertVehicleCate",vehicleCate);
    }

    @Override
    public int updateVehicleCate(VehicleCate vehicleCate) {
    	log.debug("Update for the vehicleCate parameters:"+ vehicleCate.toJson());
    	validColumnLen(vehicleCate);
     	//无code的不能更新
        if(StringUtils.isEmpty(vehicleCate.getVehicleCateCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code记录不存在
        VehicleCate oldVehicleCate = getVehicleCateByCode(vehicleCate.getVehicleCateCode());
        if(null == oldVehicleCate){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_RESULT,null);
        }
		oldVehicleCate.setCateId(vehicleCate.getCateId());
		oldVehicleCate.setBrandId(vehicleCate.getBrandId());
		oldVehicleCate.setCateCode(vehicleCate.getCateCode());
		oldVehicleCate.setCateName(vehicleCate.getCateName());
		oldVehicleCate.setManuCode(vehicleCate.getManuCode());
		oldVehicleCate.setSizeDefine(vehicleCate.getSizeDefine());
		oldVehicleCate.setCarLength(vehicleCate.getCarLength());
		oldVehicleCate.setCarWidth(vehicleCate.getCarWidth());
		oldVehicleCate.setCarHeight(vehicleCate.getCarHeight());
		oldVehicleCate.setCarWeight(vehicleCate.getCarWeight());
		oldVehicleCate.setWheelbase(vehicleCate.getWheelbase());
		oldVehicleCate.setFromGround(vehicleCate.getFromGround());
		oldVehicleCate.setModelMemo(vehicleCate.getModelMemo());
		oldVehicleCate.setModelImg(vehicleCate.getModelImg());
		oldVehicleCate.setTransportStatus(vehicleCate.getTransportStatus());
		oldVehicleCate.setTransportMode(vehicleCate.getTransportMode());
		oldVehicleCate.setVehiclePrice(vehicleCate.getVehiclePrice());
		oldVehicleCate.setPinyin(vehicleCate.getPinyin());
		oldVehicleCate.setQuanpin(vehicleCate.getQuanpin());
		oldVehicleCate.setDisplay(vehicleCate.getDisplay());
		oldVehicleCate.setDeleteMark(vehicleCate.getDeleteMark());
		oldVehicleCate.setDataSource(vehicleCate.getDataSource());
		oldVehicleCate.setCreateUser(vehicleCate.getCreateUser());
		oldVehicleCate.setCreateTime(vehicleCate.getCreateTime());
		oldVehicleCate.setUpdateUser(vehicleCate.getUpdateUser());
		oldVehicleCate.setUpdateTime(vehicleCate.getUpdateTime());
        
        return update("vehicleCate.updateVehicleCate",oldVehicleCate);
    }

    @Override
    public int deleteVehicleCate(VehicleCate vehicleCate) {
    	log.debug("Delete for the vehicleCate parameters:"+ vehicleCate.toJson());
     	//无code的不能删除
        if(StringUtils.isEmpty(vehicleCate.getVehicleCateCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        
        return delete("vehicleCate.deleteVehicleCate",vehicleCate);
    }

    @Override
    public VehicleCate getVehicleCateByCode(String code) {
    	log.debug("Query for vehicleCate code:" + code);
        return queryForObject("vehicleCate.getVehicleCateByCode",code);
    }
    @Override
    public long getVehicleCateCount(VehicleCate vehicleCate) {
    	log.debug("Query for vehicleCate count parameters:" + vehicleCate.toJson());
        return queryForObject("vehicleCate.getVehicleCateCount",vehicleCate);
    }
   @Override
    public List<VehicleCate> queryVehicleCates(VehicleCate vehicleCate, int firstRow, int lastRow) {
      	log.debug("Query for vehicleCate parameters:" + vehicleCate.toJson()+"\r\n,Page parameters:["+firstRow+","+lastRow+"]");
        if(firstRow<0 || firstRow>lastRow || lastRow<0){
            throw new RuntimeException("Digital is not logical");
        }
        if(lastRow-firstRow > MAX_RETURN_NUM){
            lastRow = firstRow + MAX_RETURN_NUM;
        }
        Map<String ,Object> param = new HashMap<String,Object>();
        param.put("vehicleCate",vehicleCate);
        param.put("firstRow",firstRow);
        param.put("rowSize",lastRow-firstRow);
        return queryList("vehicleCate.queryVehicleCates", param);
    }
    
    /**
     * 验证字段长度
     * @param vehicleCate 需要验证的对象
     */
    private void validColumnLen(VehicleCate vehicleCate){
		if(!StringUtils.isEmpty(vehicleCate.getCateId()) && vehicleCate.getCateId().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(vehicleCate.getBrandId()) && vehicleCate.getBrandId().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(vehicleCate.getCateCode()) && vehicleCate.getCateCode().length()>40){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{40},null);
        }
		if(!StringUtils.isEmpty(vehicleCate.getCateName()) && vehicleCate.getCateName().length()>80){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{80},null);
        }
		if(!StringUtils.isEmpty(vehicleCate.getManuCode()) && vehicleCate.getManuCode().length()>10){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{10},null);
        }
		if(!StringUtils.isEmpty(vehicleCate.getSizeDefine()) && vehicleCate.getSizeDefine().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(vehicleCate.getCarLength()) && vehicleCate.getCarLength().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(vehicleCate.getCarWidth()) && vehicleCate.getCarWidth().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(vehicleCate.getCarHeight()) && vehicleCate.getCarHeight().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(vehicleCate.getCarWeight()) && vehicleCate.getCarWeight().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(vehicleCate.getWheelbase()) && vehicleCate.getWheelbase().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(vehicleCate.getFromGround()) && vehicleCate.getFromGround().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(vehicleCate.getModelMemo()) && vehicleCate.getModelMemo().length()>80){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{80},null);
        }
		if(!StringUtils.isEmpty(vehicleCate.getModelImg()) && vehicleCate.getModelImg().length()>100){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{100},null);
        }
		if(!StringUtils.isEmpty(vehicleCate.getTransportStatus()) && vehicleCate.getTransportStatus().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(vehicleCate.getTransportMode()) && vehicleCate.getTransportMode().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(vehicleCate.getVehiclePrice()) && vehicleCate.getVehiclePrice().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(vehicleCate.getPinyin()) && vehicleCate.getPinyin().length()>20){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{20},null);
        }
		if(!StringUtils.isEmpty(vehicleCate.getQuanpin()) && vehicleCate.getQuanpin().length()>40){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{40},null);
        }
		if(!StringUtils.isEmpty(vehicleCate.getDisplay()) && vehicleCate.getDisplay().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
        
        //数据来源不明的不能操作
        if(!baseDataUtils.isDataSourceAvailable(vehicleCate.getDataSource())){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_DATASOURCE,null);
        }
    }
    
}
